//
//  ViewController.m
//  UI11_homework_通讯录
//
//  Created by aaaaaa on 16/6/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "Contant.h"
#import "AddressBook.h"
#import "CustomTableViewCell.h"
#import "SecondViewController.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, SecondViewControllerDelegate>

@property (nonatomic, retain) AddressBook *addressBook;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSIndexPath *indexPath;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self config];
    [self handleData];
    [self createTableView];
    
}

- (void)config{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"通讯录";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.rightBarButtonItem = [self editButtonItem];
}

- (void)handleData{
    
    self.addressBook = [[AddressBook alloc] init];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Contant" ofType:@"plist"];
    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:path];
    NSArray *keys = [[dictionary allKeys] sortedArrayUsingSelector:@selector(compare:)];
    
    for (NSString *key in keys) {
        NSArray *contantArray = [dictionary objectForKey:key];
        
        NSMutableArray *contantOfArray = [NSMutableArray array];
        
        for (NSDictionary *dic in contantArray) {
            
            Contant *contant = [[Contant alloc] init];
            [contant setValuesForKeysWithDictionary:dic];
            [contantOfArray addObject:contant];
            [contant release];
        }
        [self.addressBook.dictionary setValue:contantOfArray forKeyPath:key];
    }
    
}

- (void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - 64)];

    [self.tableView registerClass:[CustomTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
    [_tableView release];
    
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString *key = [[self.addressBook getGroupNames] objectAtIndex:section];
    NSArray *array = [self.addressBook.dictionary objectForKey:key];
    return array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    NSString *key = [[self.addressBook getGroupNames] objectAtIndex:indexPath.section];
    NSArray *array = [self.addressBook.dictionary objectForKey:key];
    
    cell.contant = array[indexPath.row];
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [[self.addressBook getGroupNames] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return  [[self.addressBook getGroupNames] objectAtIndex:section];
}

- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [self.addressBook getGroupNames];
}







/*编辑*/

- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:animated];
    
    [self.tableView setEditing:editing animated:animated];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
            return UITableViewCellEditingStyleInsert;
            break;
            
        default:
            break;
    }
    
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *key = [[self.addressBook getGroupNames] objectAtIndex:indexPath.section];
    NSMutableArray *array = [self.addressBook.dictionary objectForKey:key];
    
    switch (editingStyle) {
        case UITableViewCellEditingStyleInsert:
        {
            SecondViewController *secndVc = [[SecondViewController alloc] init];
            secndVc.delegate = self;
            [self.navigationController pushViewController:secndVc animated:YES];
            [secndVc release];
            
            self.indexPath = indexPath;
//            Contant *contant = [[Contant alloc] init];
//            contant.name = @"add";
//            contant.number = @"10011";
//            contant.header = @"01.jpg";
//            
//            [array insertObject:contant atIndex:indexPath.row];
        }
            break;
        case UITableViewCellEditingStyleDelete:
        {
            [array removeObjectAtIndex:indexPath.row];
            
            if (array.count == 0) {
                [self.addressBook.dictionary removeObjectForKey:key];
            }
        }
            break;
        default:
            break;
    }
    
    [tableView reloadData];
}


/*移动*/
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    NSString *key = [[self.addressBook getGroupNames] objectAtIndex:sourceIndexPath.section];
    NSMutableArray *array  = [self.addressBook.dictionary objectForKey:key];
    
    NSString *result = [[self.addressBook getGroupNames] objectAtIndex:destinationIndexPath.section];
    NSMutableArray *resultArray  = [self.addressBook.dictionary objectForKey:result];
    
    Contant *contant = array[sourceIndexPath.row];
    
    [contant retain];
    [array removeObjectAtIndex:sourceIndexPath.row];
    
    [resultArray insertObject:contant atIndex:destinationIndexPath.row];
    [contant release];
    
    if (array.count == 0) {
        [self.addressBook.dictionary removeObjectForKey:key];
    }
    
    [tableView reloadData];
}



- (void)backContant:(Contant *)contant{
    NSString *key = [[self.addressBook getGroupNames] objectAtIndex:self.indexPath.section];
    NSMutableArray *array = [self.addressBook.dictionary objectForKey:key];
    
    [array insertObject:contant atIndex:self.indexPath.section];
    [self.tableView reloadData];
}
















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
